package com.rem.concurrency.multithreading.state;

import java.util.concurrent.ExecutionException;

/**
 * 模拟延时:放大问题发生性
 *
 * @author Rem
 * @date 2020-10-28
 */

public class ThreadSleep implements Runnable {

    private int ticketNum = 100;

    @Override
    public void run() {
        while (true) {
            if (ticketNum <= 0) {
                break;
            }
            //模拟延时
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "拿到了第" + ticketNum-- + "张票");
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadSleep threadSleep = new ThreadSleep();


        //创建线程池
        /*ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(3,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());


        ScheduledFuture<?> schedule = executorService.schedule(threadSleep, 1, TimeUnit.MINUTES);
        schedule.get();
        executorService.shutdownNow();*/



        new Thread(threadSleep, "张三").start();
        new Thread(threadSleep, "李四").start();
        new Thread(threadSleep, "王五").start();

    }


}
